package com.game.web.controller.game;

import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.core.domain.model.LoginUser;
import com.game.common.core.page.TableDataInfo;
import com.game.common.enums.BusinessType;
import com.game.common.utils.poi.ExcelUtil;
import com.game.system.domain.game.GameAccount;
import com.game.system.service.IGameAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 游戏账号Controller
 *
 * @author Mr.Liu
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/game/account")
public class GameAccountController extends BaseController
{
    @Autowired
    private IGameAccountService gameAccountService;

    /**
     * 查询游戏账号列表
     */
    @PreAuthorize("@ss.hasPermi('game:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(GameAccount gameAccount)
    {
        startPage();
        List<GameAccount> list = gameAccountService.selectGameAccountList(gameAccount);
        return getDataTable(list);
    }

    /**
     * 导出游戏账号列表
     */
    @PreAuthorize("@ss.hasPermi('game:account:export')")
    @Log(title = "游戏账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GameAccount gameAccount)
    {
        List<GameAccount> list = gameAccountService.selectGameAccountList(gameAccount);
        ExcelUtil<GameAccount> util = new ExcelUtil<GameAccount>(GameAccount.class);
        util.exportExcel(response, list, "游戏账号数据");
    }

    /**
     * 获取游戏账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('game:account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gameAccountService.selectGameAccountById(id));
    }

    /**
     * 新增游戏账号
     */
    @PreAuthorize("@ss.hasPermi('game:account:add')")
    @Log(title = "游戏账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GameAccount gameAccount)
    {
        if (gameAccount.getGameId() == null) {
            return error("请选择游戏");
        }
        if (gameAccountService.checkGameAccountUnique(gameAccount.getAccount(), gameAccount.getGameId())) {
            return error("账号已存在");
        }
        LoginUser loginUser = getLoginUser();
        if (loginUser == null) return error("获取个人信息异常");
        // 5. 判断角色权限类型
        boolean isAgentRole = loginUser.getUser().getRoles().stream()
                .anyMatch(role -> role.getDataScope().equals("3"));
        if (isAgentRole) {
            gameAccount.setAgentId(getUserId());
        } else {
            gameAccount.setUserId(getUserId());
        }
        return toAjax(gameAccountService.insertGameAccount(gameAccount));
    }

    /**
     * 修改游戏账号
     */
    @PreAuthorize("@ss.hasPermi('game:account:edit')")
    @Log(title = "游戏账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GameAccount gameAccount)
    {
        return toAjax(gameAccountService.updateGameAccount(gameAccount));
    }

    /**
     * 删除游戏账号
     */
    @PreAuthorize("@ss.hasPermi('game:account:remove')")
    @Log(title = "游戏账号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gameAccountService.deleteGameAccountByIds(ids));
    }

    /**
     * 获取游戏账号信息
     *
     * @param gameId    游戏ID
     * @param accountId 账号ID
     */
    @GetMapping("/selectGameAccount")
    public AjaxResult getGameAccount() {
        Long gameId = getLoginUser().getUser().getGameId();
        return AjaxResult.success(gameAccountService.selectGameAccount(gameId, getUserId()));
    }
}
